<?php
class Trait_Core {
    /**
    * Take a class (a trait) and merge its methods with ours.
    * 
    * A trait is a class with no properties.  It provides methods and requires methods.
    * 
    * @param $obj Destination object
    * @param $trait Source trait
    * @param $methods Array of trait methods to be renamed:  {traitmethod => newname}
    */ 
    public function uses($obj, $trait, $methods = null) {
        $trait_reflect = new ReflectionClass($trait);
        if (sizeof($trait_reflect->getProperties())>1) throw new Exception("Traits cannot have properties");
        
        foreach($trait_reflect->getMethods() as $method) {
            $method_name = $method->getName();
            $new_name = (is_array($methods) AND in_array($method_name, $methods)) ? $methods[$method_name]:$method_name;

            if (substr($method_name,0,2) != "__")
            //$this->mTraits[$method->getName()] = $trait;
            //echo "Method added from trait: " . $method->getName() . " to class: " . get_class($this) . " from class: " . $trait . "<br>";
            runkit_method_copy($obj, $new_name, $trait, $method_name);
        }
    }
}
?>